package role

import (
	"context"

	"mall/common/modelx"
	"mall/common/utils/pointy"
	"mall/service/backend/common/errorx"
	"mall/service/backend/common/i18n"
	"mall/service/backend/rpc/internal/svc"
	"mall/service/backend/rpc/internal/utils/errorhandler"
	"mall/service/backend/rpc/types/backend"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetRoleByIdLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetRoleByIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetRoleByIdLogic {
	return &GetRoleByIdLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GetRoleByIdLogic) GetRoleById(in *backend.IDReq) (*backend.RoleInfo, error) {
	if in.Id == 0 {
		return nil, errorx.NewInvalidArgumentError(i18n.ValidationError)
	}

	res, err := l.svcCtx.SysRole.FindOne(l.ctx, in.Id)
	if err != nil {
		return nil, errorhandler.DefaultRepositoryError(l.Logger, err, in)
	}

	return &backend.RoleInfo{
		Id:            pointy.GetPointer(res.ID),
		Name:          pointy.GetPointer(res.Name),
		Code:          pointy.GetPointer(res.Code),
		Status:        pointy.GetPointer(uint32(res.Status)),
		Sort:          pointy.GetPointer(uint32(res.Sort)),
		Remark:        pointy.GetPointer(res.Remark),
		DefaultRouter: pointy.GetPointer(res.DefaultRouter),
		CreatedAt:     pointy.GetPointer(modelx.FormatTime(res.CreatedAt, "")),
		UpdatedAt:     pointy.GetPointer(modelx.FormatTime(res.UpdatedAt, "")),
		DeletedAt:     pointy.GetPointer(modelx.FormatNullDotTime(res.DeletedAt, "")),
	}, nil
}
